from pml.Parser.public import *

class If(Node):
    def __init__(self, expression=None, body=None, elseBody=None, bp=None, ep=None, depth=0):
        super().__init__('if', bp, ep, depth)
        self.expression = expression
        self.body = body
        self.elseBody = elseBody
    def __repr__(self):
        depth = '\t' * self.depth
        return 'If({})(\n{}expression={},\n{}body={},\n{}elseBody={}\n{})'.format(self.depth, depth, self.expression, depth, self.body, depth, self.elseBody, '\t' * (self.depth - 1))

    def getObject(self):
        self.methods['__init__'] = self.tp_init

        return self

    @staticmethod
    def tp_init(frame, this, position):
        pass

class IfBuilder(ASTBuilder):
    def __init__(self, tokens=None, depth=0):
        super().__init__('If', tokens, depth)

    def getObject(self):
        self.methods['__init__'] = self.tp_init

    @staticmethod
    def buildIf(frame, this: 'IfBuilder', position):
        bp = this.token.bp
        this.getNextToken()
        expressionBuilder = Parser.Expression.ExpressionBuilder(this.tokens, depth=this.depth + 1)
        expression = expressionBuilder.buildExpression(frame, expressionBuilder, position)
        del expressionBuilder
        this.updateToken()
        if this.token.type != TokenConstants.TT_KEYWORD or this.token.value != TokenConstants.Keywords.BEGIN:
            # error
            pass
        this.getNextToken()
        bodyBuilder = Parser.ASTBuilder(this.tokens, depth=this.depth + 1)
        body = bodyBuilder.buildAST(frame, bodyBuilder, position)
        ep = this.token.ep
        try:
            this.getNextToken()
        except StopIteration:
            return Objects.object.PMTypeObject(If(expression, body, None, bp, ep, depth=this.depth).getObject())
        if this.token.type != TokenConstants.TT_KEYWORD or this.token.value != TokenConstants.Keywords.ELSE:
            return Objects.object.PMTypeObject(If(expression, body, None, bp, ep, depth=this.depth).getObject())
        else:
            this.getNextToken()
            if this.token.type == TokenConstants.TT_KEYWORD and this.token.value == TokenConstants.Keywords.IF:
                elseBodyBuilder = IfBuilder(this.tokens, depth=this.depth + 1)
                elseBody = elseBodyBuilder.buildIf(frame, elseBodyBuilder, position)
            else:
                try:
                    this.getNextToken()
                except StopIteration:
                    # error
                    pass
                elseBodyBuilder = Parser.ASTBuilder(this.tokens, depth=this.depth + 1)
                elseBody = bodyBuilder.buildAST(frame, elseBodyBuilder, position)
            ep = this.token.ep
            try:
                this.getNextToken()
            except StopIteration:
                pass
            return Objects.object.PMTypeObject(If(expression, body, elseBody, bp, ep, depth=this.depth).getObject())

    @staticmethod
    def tp_init(frame, this, position):
        pass
